package com.cheyun.netsalev3.view

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.adapter.MyPagedListAdapter
import com.cheyun.netsalev3.bean.task.TaskListData
import com.cheyun.netsalev3.databinding.ActivityTaskListBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.TaskListActivityViewModel

class TaskListActivity : BaseActivity<ActivityTaskListBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_task_list
    }

    private lateinit var adapter: MyPagedListAdapter<TaskListData>

    private var searchMap = HashMap<String, String>()

    var ckey = ""
    override fun initViewModel(): ViewModel? {
        ckey = intent.getStringExtra("ckey");

        searchMap.put("ckey",ckey)
        var factory = InjectorUtils.getTaskListActivityViewModelFactory(ckey,searchMap)
        return ViewModelProviders.of(this, factory).get(TaskListActivityViewModel::class.java)
    }

    override fun initData() {
        binding.viewModel?.items?.observe(this, Observer { list ->
            list?.apply {
                adapter.submitList(list)
            }
        })


    }

    override fun initView() {

        adapter = MyPagedListAdapter(R.layout.task_list_item, binding.viewModel)
        var recyclerView = binding.root.findViewById<RecyclerView>(R.id.swipe_target)
        recyclerView.adapter = adapter
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh()
        }
    }

    override fun onResume() {
        super.onResume()

    }

}
